package com.dcxs.shop.module.category.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dcxs.shop.common.annotation.SysLog;
import com.dcxs.shop.common.annotation.UnAuthRequest;
import com.dcxs.shop.common.utils.PageUtils;
import com.dcxs.shop.common.utils.R;
import com.dcxs.shop.common.utils.RedisUtils;
import com.dcxs.shop.module.category.service.IJsCategoryThreeService;
import com.dcxs.shop.module.product.entity.*;
import com.dcxs.shop.module.product.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author zzh
 * @since 2020-03-01
 */
@RestController
@RequestMapping("/category/js-category-three")
public class JsCategoryThreeController {


    @Autowired
    private IJsCategoryThreeService categoryThreeService;

    @Autowired
    private IJsProductService productService;

    @Autowired
    private IJsProductPicService productPicService;

    @Autowired
    private IJsProductSpecificationService productSpecificationService;

    @Autowired
    private IJsProductPriceService productPriceService;

    @Autowired
    private IJsProductPayService productPayService;

    @Autowired
    private RedisUtils redisUtils;
    /**
     * 三级分类首页渲染
     */
    @UnAuthRequest
    @RequestMapping("/categoryFirstPage")
    @SysLog
    public R  categoryFirstPage(){
        List<Map> map = categoryThreeService.categoryFirstPage();
        return new R().put("data",map);
    }


    @UnAuthRequest
    @RequestMapping("/index")
    @SysLog
    public R index(){
        Map map = categoryThreeService.index();
        String date = DateUtil.formatDate(new Date());
        redisUtils.incr("jinshan:visit:pv:"+date);
        return new R().put("data",map);
    }



    /**
     * 商品列表
     */
    @UnAuthRequest
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryThreeService.queryPage(params);
        return R.ok().put("page", page);
    }

    @UnAuthRequest
    @RequestMapping("/selectOne")
    public R selectOne(@RequestParam Map<String, Object> params){
        JsProduct jsProduct = productService.getById(params.get("id").toString());
        if(1==jsProduct.getDisState()){
            if(!DateUtil.isIn(new Date(),Date.from(jsProduct.getDisBeginDatetime().atZone(ZoneId.systemDefault()).toInstant()),Date.from(jsProduct.getDisEndDatetime().atZone(ZoneId.systemDefault()).toInstant()))){
                jsProduct.setDisState(0);
            }
        }
        List<JsProductPic> jsProductPics = productPicService.list(new QueryWrapper<JsProductPic>().eq("product_id",jsProduct.getId()));
        if(CollectionUtil.isNotEmpty(jsProductPics)){
            List<String> picList = jsProductPics.stream().map(a ->a.getPicUrl()).collect(Collectors.toList());
            jsProduct.setPicList(picList);
        }
        List<JsProductSpecification> jsProductSpecifications  = productSpecificationService.list(new QueryWrapper<JsProductSpecification>().eq("product_id",jsProduct.getId()));
        if(CollectionUtil.isNotEmpty(jsProductSpecifications)){
            for(JsProductSpecification jsProductSpecification:jsProductSpecifications){
                List<JsProductPrice> priceList = productPriceService.list(new QueryWrapper<JsProductPrice>().eq("product_id",params.get("id").toString()).eq("specification_id",jsProductSpecification.getId()));
                jsProductSpecification.setJsProductPrices(priceList);
            }
            jsProduct.setSpecificationList(jsProductSpecifications);
        }
        String count = redisUtils.get("jinshan:product:sale:"+params.get("id").toString());
        redisUtils.incr("jinshan:product:pv:"+params.get("id").toString());
        if(StringUtils.isNotBlank(count)){
            jsProduct.setSaleCount(Integer.valueOf(count));
        }else {
            jsProduct.setSaleCount(0);
        }
        //查询支付方式
        List<JsProductPay> list = productPayService.list(new QueryWrapper<JsProductPay>().eq("product_id",params.get("id").toString()));
        if(CollectionUtil.isNotEmpty(list)){
            jsProduct.setPayList(list);
        }
        return R.ok().put("data", jsProduct);
    }
}
